﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;
        let $ = layui.$;

        var openWH = miniPage.getOpenWidthHeight();

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysBasic/Role/List",
            limit: 10,
            page: true,
            defaultToolbar: ['exports', 'filter'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "EnCode", title: "角色编码", sort: true, width: layui.device().mobile ? 150 : null },
                { field: "FullName", title: "角色名称", sort: true, width: layui.device().mobile ? 200 : null },
                { title: "菜单权限", width: 120, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTp1" },
                { title: "操作", width: 120, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTp2" }
            ]],
            done: function (res, curr, count) {
                if (layui.device().mobile) {
                    $("#add").html("<i class='fa fa-plus'></i>新建");
                }
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);
                    break;
                case "del":
                    del(data.Id);
                    break;
                case "assign":
                    assign(data.Id, data.FullName);
                    break;
            }
        });

        function add() {
            exLayer.openMiddle("新建角色", "/SysBasic/Role/Add", '500px', '450px', layui.device().mobile);
        }

        function edit(id) {
            exLayer.openMiddle("编辑角色", "/SysBasic/Role/Edit/" + id, '500px', '450px', layui.device().mobile);
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysBasic/Role/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function assign(id, name) {
            exLayer.openMiddle(name + " 菜单权限", "/SysBasic/Role/Assign/" + id, '600px', '400px', layui.device().mobile);
        }
    });
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" id="add"><i class="fa fa-plus"></i>新建角色</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTp1">
    <a href='javascript:;' class='layui-btn layui-btn-xs layui-btn-normal' lay-event='assign' id='assign'>菜单权限</a>
</script>
<script type="text/html" id="operationTp2">
    <a href='javascript:;' class='layui-btn layui-btn-normal layui-btn-xs ' lay-event='edit' id='edit'>编辑</a>
    <a href='javascript:;' class='layui-btn layui-btn-xs layui-btn-danger' lay-event='del' id='del'>删除</a>
</script>